<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 会员管理
// +----------------------------------------------------------------------
namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Goodsattr extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $id = $this->request->param('goods_id');
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['is_del', '=', 0];
            $where[] = ['goods_id', '=', $id];
            $_list = Db::name('goods_attribute_value')
                ->where($where)
                ->page($page, $limit)
                ->order('sort desc')
                ->select();
            $total = Db::name('goods_attribute_value')->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'goods_id' => $id
        ]);
        return $this->fetch();
    }

    public function add()
    {
        $goods_id = $this->request->param('goods_id');
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'goods_id' => $data['goods_id'],
                'name' => $data['name'],
                'value' => $data['value'],
                'sort' => $data['sort'],
            );
            $stmt = Db::name('goods_attribute_value')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $this->assign([
            'goods_id'=>$goods_id
        ]);
        return $this->fetch();

    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('goods_attribute_value')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'name' => $data['name'],
                'value' => $data['value'],
                'sort' => $data['sort']
            );
            $stmt = Db::name('goods_attribute_value')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('goods_attribute_value')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('goods_attribute_value')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }


}
